package io;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;

import java.io.IOException;
import java.util.stream.Stream;

public class ResourcePatternResolverDemo {

    public static void main(String[] args) throws IOException {
        PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        System.out.println("PathMatcher:" + resolver.getPathMatcher().getClass());
        //类路径下io目录所有class,包括子目录
        Resource[] resources = resolver.getResources("classpath:io/**.class");
        outputResource(resources);

        //类路径下test.log文件
        resources = resolver.getResources("classpath:test.log");
        outputResource(resources);

//        //一个绝对路径
//        resources = resolver.getResources(System.getProperty("user.dir")+
//                "\\think-in-spring\\spring-resource\\src\\main\\java\\io\\SpringResourceDemo.java");
//        outputResource(resources);

        resources = resolver.getResources("classpath:/path");
        outputResource(resources);

    }

    private static void outputResource(Resource[] resources) {
        Stream.of(resources).map(e->{
            try {
               return e.getFile().getAbsolutePath();
            } catch (IOException ioException) {
                ioException.printStackTrace();
                return "exception";
            }
        }).forEach(System.out::println);
    }
}
